<?php
/*
  $Id: ipayment.php,v 1.32 2003/01/29 19:57:14 hpdl Exp $

  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2003 osCommerce

  Released under the GNU General Public License
*/
  //define("DAB_NAMESPACE", 'https://ws.onlink.vn/gateway');
  define("CACHE_PATH", '/tmp/pear/cache/');
  define("MY_CAPATH" ,'/usr/local/ssl/certs');
  ini_set('soap.wsdl_cache','0');
  ini_set('soap.wsdl_cache_enabled','0');
  require_once DIR_FS_CATALOG . DIR_WS_CLASSES . 'NuSOAP/nusoap.php';
  
  class onlinkpayment {
    var $code, $title, $description, $enabled;
	var $require_field;
	var $card_issuer_info;
	var $transactionTypeID;
	var $paymentMethod;

// class constructor
    function onlinkpayment() {
      global $order;

	  $this->require_field[0]['title'] = 'Ngân hàng Đông Á';
	  $this->require_filed[0]['fields'][] = 'onlinkpayment_card_holder_name';
	  $this->require_filed[0]['fields'][] = 'onnlinkpayment_expires_month';
	  $this->require_filed[0]['fields'][] = 'onlinkpayment_expires_year';
	  $this->require_filed[0]['fields'][] = 'onlinkpayment_card_number';
	  
	  $this->requier_field[1]['title'] = 'Ngân hàng Liên Việt';
	  $this->require_filed[1]['fields'][] = 'onlinkpayment_card_holder_name';
	  $this->require_filed[1]['fields'][] = 'onlinkpayment_card_holder_id';
	  $this->require_filed[1]['fields'][] = 'onlinkpayment_card_holder_cif';
	  $this->require_filed[1]['fields'][] = 'onlinkpayment_card_number';

	  $soapClient = new nusoap_client(MODULE_PAYMENT_ONLINKPAYMENT_WSDLWEBSERVICE, true, false, array(), CACHE_PATH);
	  $soapClient->soap_defencoding = 'UTF-8';
	  $soapClient->decode_utf8 = false;
	  $soapClient->setCurlOption("timeout", 100);
	  $soapClient->setCurlOption('curl', CURLOPT_SSL_VERIFYHOST, 0);
	  $soapClient->setCurlOption('curl', CURLOPT_SSL_VERIFYPEER, 0);
	  $soapClient->setCurlOption('curl', CURLOPT_CAPATH, MY_CAPATH);
	  //$soapOptions = array('namespace' =>DAB_NAMESPACE, 'trace' => 1);//var_dump($soapClient);
	  $cardIssuersInfoReturn= $soapClient->call("getCardIssuers",array("merchantID"=>MODULE_PAYMENT_IPAYMENT_MERCHANT_ID,"merchantKey"=>MODULE_PAYMENT_MERCHANT_KEY,"name"=>"Vu Nguyen Hoang"));
	  //echo '<b>Goi ham : $soapClient->call("getCardIssuers",array("merchantID"=>MODULE_PAYMENT_IPAYMENT_MERCHANT_ID,"merchantKey"=>MODULE_PAYMENT_MERCHANT_KEY,"name"=>"Vu Nguyen Hoang"));</b></br>Ket qua tra ve : </br>';
	  //var_dump($cardIssuersInfoReturn);
	  //$hello = $soapClient->call("hello",array("merchantID"=>MODULE_PAYMENT_IPAYMENT_MERCHANT_ID,"merchantKey"=>MODULE_PAYMENT_MERCHANT_KEY,"name"=>"Vu Nguyen Hoang"));
	  //echo '<br><b>Goi ham : //$soapClient->call("hello",array("merchantID"=>MODULE_PAYMENT_IPAYMENT_MERCHANT_ID,"merchantKey"=>MODULE_PAYMENT_MERCHANT_KEY,"name"=>"Vu Nguyen Hoang"));</b></br>Ket qua tra ve : </br>';
	  //var_dump($hello);	  
	  foreach ($cardIssuersInfoReturn['cardIssuerInfoList'] as $cardIssuerInfo) {
		$this->card_issuer_info[] = array('id' => $cardIssuerInfo['cardID'], 'text'=> $cardIssuerInfo['cardName']);		
	  }	  

	  $this->code = 'onlinkpayment';
      $this->title = MODULE_PAYMENT_ONLINKPAYMENT_TEXT_TITLE;
      $this->description = MODULE_PAYMENT_ONLINKPAYMENT_TEXT_DESCRIPTION;
      $this->sort_order = MODULE_PAYMENT_ONLINKPAYMENT_SORT_ORDER;
      $this->enabled = ((MODULE_PAYMENT_ONLINKPAYMENT_STATUS == 'True') ? true : false);

      if ((int)MODULE_PAYMENT_ONLINKPAYMENT_ORDER_STATUS_ID > 0) {
        $this->order_status = MODULE_PAYMENT_ONLINKPAYMENT_ORDER_STATUS_ID;
      }

      if (is_object($order)) $this->update_status();

      $this->form_action_url = FILENAME_ONLINK_PROCESS_OKKO;
    }

// class methods
    function update_status() {
      global $order;

      if ( ($this->enabled == true) && ((int)MODULE_PAYMENT_ONLINKPAYMENT_ZONE > 0) ) {
        $check_flag = false;
        $check_query = tep_db_query("select zone_id from " . TABLE_ZONES_TO_GEO_ZONES . " where geo_zone_id = '" . MODULE_PAYMENT_ONLINKPAYMENT_ZONE . "' and zone_country_id = '" . $order->billing['country']['id'] . "' order by zone_id");
        while ($check = tep_db_fetch_array($check_query)) {
          if ($check['zone_id'] < 1) {
            $check_flag = true;
            break;
          } elseif ($check['zone_id'] == $order->billing['zone_id']) {
            $check_flag = true;
            break;
          }
        }

        if ($check_flag == false) {
          $this->enabled = false;
        }
      }
    }

    function javascript_validation() {
      $js = '  if (payment_value == "' . $this->code . '") { ' . "\n" .
            '    var onlinkpayment_card_holder_name = document.checkout_payment.onlinkpayment_card_holder_name.value;' . "\n" .
            '    var onlinkpayment_card_number = document.checkout_payment.onlinkpayment_card_number.value;' . "\n" .
            '    if (onlinkpayment_card_holder_name == "" || onlinkpayment_card_holder_name.length < ' . CC_OWNER_MIN_LENGTH . ') {' . "\n" .
            '      error_message = error_message + "' . MODULE_PAYMENT_ONLINKPAYMENT_TEXT_JS_CC_OWNER . '";' . "\n" .
            '      error = 1;' . "\n" .
            '    }' . "\n" .
            '    if (onlinkpayment_card_number == "" || onlinkpayment_card_number.length < ' . CC_NUMBER_MIN_LENGTH . ') {' . "\n" .
            '      error_message = error_message + "' . MODULE_PAYMENT_ONLINKPAYMENT_TEXT_JS_CC_NUMBER . '";' . "\n" .
            '      error = 1;' . "\n" .
            '    }' . "\n" .
		    '    if (document.checkout_payment.onlinkpayment_card_issuer == 1) {' . "\n" .
		    '      var onlinkpayment_card_holder_id = document.checkout_payment.onlinkpayment_card_holder_id.value;' . "\n" .
		    '      var onlinkpayment_card_holder_cif = document.checkout_payment.onlinkpayment_card_holder_cif.value;' . "\n" .
            '      if (onlinkpayment_card_holder_id == "") {' . "\n" .
            '        error_message = error_message + "' . MODULE_PAYMENT_ONLINKPAYMENT_TEXT_JS . ' ' . MODULE_PAYMENT_ONNLINKPAYMENT_TEXT_CARD_HOLDER_ID . '";' . "\n" .
            '        error = 1;' . "\n" .
            '      }' . "\n" .
            '      if (onlinkpayment_card_holder_cif == "") {' . "\n" .
            '        error_message = error_message + "' . MODULE_PAYMENT_ONLINKPAYMENT_TEXT_JS . ' ' . MODULE_PAYMENT_ONLINKPAYMENT_TEXT_CARD_HOLDER_CIF . '";' . "\n" .
            '        error = 1;' . "\n" .
            '      }' . "\n" .

            '    }' . "\n" . 
            '  }' . "\n";

      return $js;
    }

    function selection() {
      global $order;		  	  
	  $today = getdate(); 
	  $today_mon = sprintf('%02d', $today['mon']);
	  $today_year = strftime('%y',mktime(0,0,0,1,1,$today['year']));
	  for ($i=1; $i < 13; $i++) {
        //$expires_month[] = array('id' => sprintf('%02d', $i), 'text' => strftime('%B',mktime(0,0,0,$i,1,2000)));
		$expires_month[] = array('id' => sprintf('%02d', $i), 'text' => $i);
      }

      for ($i=$today['year'] - 10; $i < $today['year']+10; $i++) {
        $expires_year[] = array('id' => strftime('%y',mktime(0,0,0,1,1,$i)), 'text' => strftime('%Y',mktime(0,0,0,1,1,$i)));
      }

	  
	  
      $selection = array('id' => $this->code,
                         'module' => $this->title,
                         'fields' => array(array('title' => MODULE_PAYMENT_ONLINKPAYMENT_TEXT_CARD_ISSUER,
                                                 'field' => tep_draw_pull_down_menu('onlinkpayment_card_issuer', $this->card_issuer_info)),
										   array('title' => MODULE_PAYMENT_ONLINKPAYMENT_TEXT_ACB_TEMS_TITLE,
                                                 'field' => MODULE_PAYMENT_ONLINKPAYMENT_TEXT_ACB_TERMS),
										   array('title' => MODULE_PAYMENT_ONLINKPAYMENT_TEXT_CARD_HOLDER_NAME,
                                                 'field' => tep_draw_input_field('onlinkpayment_card_holder_name', strtoupper( remove_vietnamese_char($order->billing['lastname'] . ' ' . $order->billing['firstname'])), 'id="onlinkpayment_card_holder_name"')),
                                           array('title' => MODULE_PAYMENT_ONNLINKPAYMENT_TEXT_CARD_HOLDER_ID,
                                                 'field' => tep_draw_input_field('onlinkpayment_card_holder_id')),
										   array('title' => MODULE_PAYMENT_ONLINKPAYMENT_TEXT_CARD_HOLDER_CIF,
                                                 'field' => tep_draw_input_field('onlinkpayment_card_holder_cif')),
										   array('title' => MODULE_PAYMENT_ONLINKPAYMENT_TEXT_CARD_NUMBER,
                                                 'field' => tep_draw_input_field('onlinkpayment_card_number')),
                                           array('title' => MODULE_PAYMENT_ONLINKPAYMENT_TEXT_CARD_EXPIRES,
                                                 'field' => tep_draw_pull_down_menu('onnlinkpayment_expires_month', $expires_month, $today_mon) . '&nbsp;' . tep_draw_pull_down_menu('onlinkpayment_expires_year', $expires_year, $today_year)),
										   array('title' => MODULE_PAYMENT_ONLINKPAYMENT_TEXT_CARD_ADDRESS,
                                                 'field' => tep_draw_input_field('onlinkpayment_card_address')),
										   array('title' => MODULE_PAYMENT_ONLINKPAYMENT_TEXT_CARD_CVV,
                                                 'field' => tep_draw_input_field('onlinkpayment_card_cvv')),
										   array('title' => MODULE_PAYMENT_ONLINKPAYMENT_TEXT_CARD_BILLING_NAME,
                                                 'field' => tep_draw_input_field('onlinkpayment_card_billing_name')),
										   array('title' => MODULE_PAYMENT_ONLINKPAYMENT_TEXT_CARD_BILLING_ID,
                                                 'field' => tep_draw_input_field('onlinkpayment_card_billing_id')),
										   array('title' => MODULE_PAYMENT_ONLINKPAYMENT_TEXT_CARD_BILLING_ADDRESS,
                                                 'field' => tep_draw_input_field('onlinkpayment_card_billing_address', $order->billing['stress_address'] . ' ' . $order->billing['city'] . ' ' . $order->billing['country']['title'])),
										   array('title' => MODULE_PAYMENT_ONLINKPAYMENT_TEXT_CARD_BILLING_PHONE,
                                                 'field' => tep_draw_input_field('onlinkpayment_card_billing_phone', $order->customer['telephone'])),
		  								   array('title' => MODULE_PAYMENT_ONLINKPAYMENT_TEXT_CARD_BILLING_EMAIL,
                                                 'field' => tep_draw_input_field('onlinkpayment_card_billing_email', $order->customer['email_address'])),
										   array('title' => MODULE_PAYMENT_ONLINKPAYMENT_TEXT_SHIPPPING_NAME,
                                                 'field' => tep_draw_input_field('onlinkpayment_card_shipping_name', $order->delivery['lastname'] . ' ' . $order->delivery['firstname'])),
										   array('title' => MODULE_PAYMENT_ONLINKPAYMENT_TEXT_CARD_SHIPPING_ID,
                                                 'field' => tep_draw_input_field('onlinkpayment_card_shipping_id')),
										   array('title' => MODULE_PAYMENT_ONLINKPAYMENT_TEXT_CARD_SHIPPING_ADDRESS,
                                                 'field' => tep_draw_input_field('onlinkpayment_card_shipping_address', $order->delivery['stress_address'] . ' ' . $order->delivery['city'] . ' ' . $order->delivery['country']['title'])),
										   array('title' => MODULE_PAYMENT_ONLINKPAYMENT_TEXT_CARD_SHIPPING_PHONE,
                                                 'field' => tep_draw_input_field('onlinkpayment_card_shipping_phone', $order->customer['telephone'])),
										   array('title' => MODULE_PAYMENT_ONLINKPAYMENT_TEXT_CARD_SHIPPING_EMAIL,
                                                 'field' => tep_draw_input_field('onlinkpayment_card_shipping_email', $order->customer['email_address']))));
	  $selection = array('id' => $this->code,
                         'module' => $this->title,
                         'fields' => array(array('title' => MODULE_PAYMENT_ONLINKPAYMENT_TEXT_CARD_ISSUER,
                                                 'field' => tep_draw_pull_down_menu('onlinkpayment_card_issuer', $this->card_issuer_info)),
										   array('title' => MODULE_PAYMENT_ONLINKPAYMENT_TEXT_ACB_TEMRS_TITLE,
                                                 'field' => MODULE_PAYMENT_ONLINKPAYMENT_TEXT_ACB_TERMS),
										   array('title' => '',
                                                 'field' => tep_draw_checkbox_field('onlinkpayment_terms_agree', 1, $payment_info['onlinkpayment_terms_agree'] == "1" ? true : false,  'id="onlinkpayment_terms_agree"').MODULE_PAYMENT_ONLINKPAYMENT_TEXT_ACB_TEMRS_AGREE),										   
                                           array('title' => MODULE_PAYMENT_ONLINKPAYMENT_TEXT_CARD_NUMBER,
                                                 'field' => tep_draw_input_field('onlinkpayment_card_number_1', $payment_info['onlinkpayment_card_number_1'], 'id="onlinkpayment_card_number_1" maxlength="4" style="width:30px"').' - '.tep_draw_input_field('onlinkpayment_card_number_2', $payment_info['onlinkpayment_card_number_2'], 'id="onlinkpayment_card_number_2" maxlength="4" style="width:30px"').' - '.tep_draw_input_field('onlinkpayment_card_number_3', $payment_info['onlinkpayment_card_number_3'], 'id="onlinkpayment_card_number_3" maxlength="4" style="width:30px"').' - '.tep_draw_input_field('onlinkpayment_card_number_4', $payment_info['onlinkpayment_card_number_4'], 'id="onlinkpayment_card_number_4" maxlength="4" style="width:30px"') . '  VD: 1792 - 0102 - 1020 - 4797'),
                                           array('title' => MODULE_PAYMENT_ONLINKPAYMENT_TEXT_CARD_HOLDER_NAME,
                                                 'field' => tep_draw_input_field('onlinkpayment_card_holder_name', $payment_info['onlinkpayment_card_holder_name'] != ''?$payment_info['onlinkpayment_card_holder_name']:strtoupper( remove_vietnamese_char($order->billing['lastname'] . ' ' . $order->billing['firstname'])), 'id="onlinkpayment_card_holder_name"') . '  VD: BUI NAM CUONG'),
										   array('title' => MODULE_PAYMENT_ONLINKPAYMENT_PAYMENT_METHOD_TITLE,
                                                 'field' => tep_draw_radio_field('onlinkpayment_payment_method', 'INTERNET', true, 'id="onlinkpayment_payment_method_internet"').MODULE_PAYMENT_ONLINKPAYMENT_PAYMENT_METHOD_INTERNET.'</br>'.tep_draw_radio_field('onlinkpayment_payment_method', 'SMS', $payment_info['onlinkpayment_payment_method'] === "SMS" ? true : false, 'id="onlinkpayment_payment_method_sms"').MODULE_PAYMENT_ONLINKPAYMENT_PAYMENT_METHOD_SMS)
										   ));
		//print_r( $order);
      return $selection;
    }

    function pre_confirmation_check() {
      global $HTTP_POST_VARS;
	
      include(DIR_WS_CLASSES . 'cc_validation.php');
	  
	  /*
      $cc_validation = new cc_validation();
      $result = $cc_validation->validate($HTTP_POST_VARS['onlinkpayment_card_number_1'].$HTTP_POST_VARS['onlinkpayment_card_number_2'].$HTTP_POST_VARS['onlinkpayment_card_number_3'].$HTTP_POST_VARS['onlinkpayment_card_number_4'], $HTTP_POST_VARS['onnlinkpayment_expires_month'], $HTTP_POST_VARS['onlinkpayment_expires_year']);

      $error = '';
      switch ($result) {
        case -1:
          $error = sprintf(TEXT_CCVAL_ERROR_UNKNOWN_CARD, substr($cc_validation->cc_number, 0, 4));
          break;
        case -2:
        case -3:
        case -4:
          $error = TEXT_CCVAL_ERROR_INVALID_DATE;
          break;
        case false:
          $error = TEXT_CCVAL_ERROR_INVALID_NUMBER;
          break;
      }

      if ( ($result == false) || ($result < 1) ) {
        $payment_error_return = 'payment_error=' . $this->code . '&error=' . urlencode($error);

        tep_redirect(tep_href_link(FILENAME_CHECKOUT_PAYMENT, $payment_error_return, 'SSL', true, false));
      }

      $this->cc_card_type = $cc_validation->cc_type;
      $this->cc_card_number = $cc_validation->cc_number;
      $this->cc_expiry_month = $cc_validation->cc_expiry_month;
      $this->cc_expiry_year = $cc_validation->cc_expiry_year;*/
	  $this->tep_session_register_payment_info();
	  if ($HTTP_POST_VARS['onlinkpayment_terms_agree'] != 1) {
			$payment_error_return = 'payment_error=' . $this->code . '&error=' . urlencode($result['errorMessage']['VIE']). 
				'&onlinkpayment_card_holder_name=' . $HTTP_POST_VARS['onlinkpayment_card_holder_name'] .
				'&onlinkpayment_card_number_1=' . $HTTP_POST_VARS['onlinkpayment_card_number_1'] .
				'&onlinkpayment_card_number_2=' . $HTTP_POST_VARS['onlinkpayment_card_number_2'] .
				'&onlinkpayment_card_number_3=' . $HTTP_POST_VARS['onlinkpayment_card_number_3'] .
				'&onlinkpayment_card_number_4=' . $HTTP_POST_VARS['onlinkpayment_card_number_4'] .
				'&onnlinkpayment_expires_month=' . $HTTP_POST_VARS['onnlinkpayment_expires_month'] .
				'&onlinkpayment_expires_year=' . $HTTP_POST_VARS['onlinkpayment_expires_year'].
				'&onlinkpayment_terms_agree=' . $HTTP_POST_VARS['onlinkpayment_terms_agree'];
	        tep_redirect(tep_href_link(FILENAME_CHECKOUT_PAYMENT, $payment_error_return, 'SSL', true, false));
	  }
	  $cardInfos = array(
					'cardID' => $HTTP_POST_VARS['onlinkpayment_card_issuer'],
					'cardHolderName' => $HTTP_POST_VARS['onlinkpayment_card_holder_name'],
					'cardHolderID' => $HTTP_POST_VARS['onlinkpayment_card_holder_id'],
					'cardHolderCIF' => $HTTP_POST_VARS['onlinkpayment_card_holder_cif'],
					'cardNumber' => $HTTP_POST_VARS['onlinkpayment_card_number_1'].$HTTP_POST_VARS['onlinkpayment_card_number_2'].$HTTP_POST_VARS['onlinkpayment_card_number_3'].$HTTP_POST_VARS['onlinkpayment_card_number_4'],
					'cardExpireDate' => '',
					'originalCurrencyShortName' => "VND");
	  
	   $soapClient = new nusoap_client(MODULE_PAYMENT_ONLINKPAYMENT_WSDLWEBSERVICE, true, false, array(), CACHE_PATH);
	   $soapClient->soap_defencoding = 'UTF-8';
	   $soapClient->decode_utf8 = false;
	   $soapClient->setCurlOption("timeout", 100);
	   $soapClient->setCurlOption('curl', CURLOPT_SSL_VERIFYHOST, 0);
	   $soapClient->setCurlOption('curl', CURLOPT_SSL_VERIFYPEER, 0);
	   $soapClient->setCurlOption('curl', CURLOPT_CAPATH, MY_CAPATH);
	   $soapOptions = array('namespace' =>DAB_NAMESPACE, 'trace' => 1);//var_dump($soapClient);
	   $result= $soapClient->call("checkCard",array("merchantID"=>MODULE_PAYMENT_IPAYMENT_MERCHANT_ID,"merchantKey"=>MODULE_PAYMENT_MERCHANT_KEY, "cardInfo"=>$cardInfos),$soapOptions);
	   //echo '<b>Goi ham : $soapClient->call("checkCard",array("merchantID"=>MODULE_PAYMENT_IPAYMENT_MERCHANT_ID,"merchantKey"=>MODULE_PAYMENT_MERCHANT_KEY, "cardInfo"=>$cardInfos),$soapOptions);</b></br>';
	   //echo '<b>Trong do noi dung bien cardInfo la : <br>';
	   //var_dump($cardInfos);exit;
	   if ($result['errorCode'] < 0) {
		    //var_dump($cardInfos);exit;
		    //var_dump($result['errorMessage']);exit;
			$payment_error_return = 'payment_error=' . $this->code . '&error=' . urlencode($result['errorMessage']['VIE']). 
				'&onlinkpayment_card_holder_name=' . $HTTP_POST_VARS['onlinkpayment_card_holder_name'] .
				'&onlinkpayment_card_number_1=' . $HTTP_POST_VARS['onlinkpayment_card_number_1'] .
				'&onlinkpayment_card_number_2=' . $HTTP_POST_VARS['onlinkpayment_card_number_2'] .
				'&onlinkpayment_card_number_3=' . $HTTP_POST_VARS['onlinkpayment_card_number_3'] .
				'&onlinkpayment_card_number_4=' . $HTTP_POST_VARS['onlinkpayment_card_number_4'] .
				'&onnlinkpayment_expires_month=' . $HTTP_POST_VARS['onnlinkpayment_expires_month'] .
				'&onlinkpayment_expires_year=' . $HTTP_POST_VARS['onlinkpayment_expires_year'] .
				'&onlinkpayment_terms_agree=' . $HTTP_POST_VARS['onlinkpayment_terms_agree'];
	        tep_redirect(tep_href_link(FILENAME_CHECKOUT_PAYMENT, $payment_error_return, 'SSL', true, false));
	   }
	   $this->cc_card_type = $cc_validation->cc_type;
       $this->cc_card_number = $HTTP_POST_VARS['onlinkpayment_card_number_1'].$HTTP_POST_VARS['onlinkpayment_card_number_2'].$HTTP_POST_VARS['onlinkpayment_card_number_3'].$HTTP_POST_VARS['onlinkpayment_card_number_4'];
       $this->cc_expiry_month = $HTTP_POST_VARS['onnlinkpayment_expires_month'];
       $this->cc_expiry_year = $HTTP_POST_VARS['onnlinkpayment_expires_year'];
    }

    function confirmation() {
      global $HTTP_POST_VARS;

      $confirmation = array('title' => $this->title . ': ' . $this->card_issuer_info[$HTTP_POST_VARS['onlinkpayment_card_issuer']]['text'],
                            'fields' => array(array('title' => MODULE_PAYMENT_ONLINKPAYMENT_TEXT_CARD_HOLDER_NAME,
                                                    'field' => $HTTP_POST_VARS['onlinkpayment_card_holder_name']),
                                              array('title' => MODULE_PAYMENT_ONLINKPAYMENT_TEXT_CARD_NUMBER,
                                                    'field' => substr($this->cc_card_number, 0, 4) . str_repeat('X', (strlen($this->cc_card_number) - 8)) . substr($this->cc_card_number, -4)),
                                              array('title' => MODULE_PAYMENT_ONLINKPAYMENT_TEXT_CARD_EXPIRES,
                                                    'field' => strftime('%B, %Y', mktime(0,0,0,$HTTP_POST_VARS['onnlinkpayment_expires_month'], 1, '20' . $HTTP_POST_VARS['onlinkpayment_expires_year'])))));

      return $confirmation;
    }

    function process_button() {
      global $HTTP_POST_VARS, $order, $currencies, $currency;

      switch (MODULE_PAYMENT_ONLINKPAYMENT_CURRENCY) {
        case 'Always VND':
          $trx_currency = 'VND';
          break;
        case 'Always USD':
          $trx_currency = 'USD';
          break;
        case 'Either VND or USD, else EUR':
          if ( ($currency == 'VND') || ($currency == 'USD') ) {
            $trx_currency = $currency;
          } else {
            $trx_currency = 'VND';
          }
          break;
        case 'Either VND or USD, else USD':
          if ( ($currency == 'VND') || ($currency == 'USD') ) {
            $trx_currency = $currency;
          } else {
            $trx_currency = 'VND';
          }
          break;
      }
	  $this->tep_session_register_payment_info();
      $process_button_string = tep_draw_hidden_field('onlinkpayment_card_issuer', $HTTP_POST_VARS['onlinkpayment_card_issuer']) .
							   tep_draw_hidden_field('onlinkpayment_card_holder_name', $HTTP_POST_VARS['onlinkpayment_card_holder_name']) .
							   tep_draw_hidden_field('onlinkpayment_card_holder_id', $HTTP_POST_VARS['onlinkpayment_card_holder_id']) .
							   tep_draw_hidden_field('onlinkpayment_card_holder_cif', $HTTP_POST_VARS['onlinkpayment_card_holder_cif']) .
					    	   tep_draw_hidden_field('onlinkpayment_card_number', $HTTP_POST_VARS['onlinkpayment_card_number_1'].$HTTP_POST_VARS['onlinkpayment_card_number_2'].$HTTP_POST_VARS['onlinkpayment_card_number_3'].$HTTP_POST_VARS['onlinkpayment_card_number_4']) .
							   tep_draw_hidden_field('onlinkpayment_card_number_1', $HTTP_POST_VARS['onlinkpayment_card_number_1']) .
		                       tep_draw_hidden_field('onlinkpayment_card_number_2', $HTTP_POST_VARS['onlinkpayment_card_number_2']) .
		                       tep_draw_hidden_field('onlinkpayment_card_number_3', $HTTP_POST_VARS['onlinkpayment_card_number_3']) .
		                       tep_draw_hidden_field('onlinkpayment_card_number_4', $HTTP_POST_VARS['onlinkpayment_card_number_4']) .
							   tep_draw_hidden_field('onnlinkpayment_expires_month', $HTTP_POST_VARS['onnlinkpayment_expires_month']) .
                    		   tep_draw_hidden_field('onlinkpayment_expires_year', $HTTP_POST_VARS['onlinkpayment_expires_year']) .
							   tep_draw_hidden_field('onlinkpayment_card_address', $HTTP_POST_VARS['onlinkpayment_card_address']) .
                    		   tep_draw_hidden_field('onlinkpayment_card_cvv', $HTTP_POST_VARS['onlinkpayment_card_cvv']) .
							   tep_draw_hidden_field('onlinkpayment_card_billing_name', $HTTP_POST_VARS['onlinkpayment_card_billing_name']) .
                     		   tep_draw_hidden_field('onlinkpayment_card_billing_id', $HTTP_POST_VARS['onlinkpayment_card_billing_id']) .
							   tep_draw_hidden_field('onlinkpayment_card_billing_address', $HTTP_POST_VARS['onlinkpayment_card_billing_address']) .
		                       tep_draw_hidden_field('onlinkpayment_card_billing_phone', $HTTP_POST_VARS['onlinkpayment_card_billing_phone']) .
							   tep_draw_hidden_field('onlinkpayment_card_billing_email', $HTTP_POST_VARS['onlinkpayment_card_billing_email']) .
                    		   tep_draw_hidden_field('onlinkpayment_card_shipping_name', $HTTP_POST_VARS['onlinkpayment_card_shipping_name']) .
							   tep_draw_hidden_field('onlinkpayment_card_shipping_id', $HTTP_POST_VARS['onlinkpayment_card_shipping_id']) .
                     		   tep_draw_hidden_field('onlinkpayment_card_shipping_address', tep_address_format($order->delivery['format_id'], $order->delivery, 1, ' ', ' ')) .
							   tep_draw_hidden_field('onlinkpayment_card_shipping_phone', $HTTP_POST_VARS['onlinkpayment_card_shipping_phone']) .
							   tep_draw_hidden_field('onlinkpayment_card_shipping_email', $HTTP_POST_VARS['onlinkpayment_card_shipping_email']) .
							   tep_draw_hidden_field('onlinkpayment_payment_method', $HTTP_POST_VARS['onlinkpayment_payment_method']).
							   tep_draw_hidden_field('onlinkpayment_original_current_short_name', "VND") .
                               tep_draw_hidden_field('onlinkpayment_original_amount', $order->info['total']) .
							   tep_draw_hidden_field('redirect_url', tep_href_link(FILENAME_CHECKOUT_PROCESS, '', 'SSL', true)) .
                               tep_draw_hidden_field('silent_error_url', tep_href_link(FILENAME_CHECKOUT_PAYMENT, 'payment_error=' . $this->code . '&onlinkpayment_card_holder_name=' . urlencode($HTTP_POST_VARS['onlinkpayment_card_holder_name']), 'SSL', true));	  	  
      return $process_button_string;
    }

    function before_process() {
      return false;
    }

    function after_process() {
      return false;
    }

    function get_error() {
      global $HTTP_GET_VARS;

      $error = array('title' => IPAYMENT_ERROR_HEADING,
                     'error' => ((isset($HTTP_GET_VARS['error'])) ? stripslashes(urldecode($HTTP_GET_VARS['error'])) : IPAYMENT_ERROR_MESSAGE));

      return $error;
    }

    function check() {
      if (!isset($this->_check)) {
        $check_query = tep_db_query("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_PAYMENT_ONLINKPAYMENT_STATUS'");
        $this->_check = tep_db_num_rows($check_query);
      }
      return $this->_check;
    }

    function install() {
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Enable OnlinkPayment Module', 'MODULE_PAYMENT_ONLINKPAYMENT_STATUS', 'True', 'Do you want to accept OnlinkPayment payments?', '6', '1', 'tep_cfg_select_option(array(\'True\', \'False\'), ', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Merchant ID', 'MODULE_PAYMENT_IPAYMENT_MERCHANT_ID', '17', 'The merchand ID for the onlinkPayment service', '6', '3', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Merchant key', 'MODULE_PAYMENT_MERCHANT_KEY', '9c2e95AhJDQPQ7aXT3KD', 'The merchant key for the onlinkPayment service', '6', '4', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Transaction Currency', 'MODULE_PAYMENT_ONLINKPAYMENT_CURRENCY', 'Either VND or USD, else VND', 'The currency to use for onlink card transactions', '6', '5', 'tep_cfg_select_option(array(\'Always VND\', \'Always USD\', \'Either VND or USD, else VND\', \'Either VND or USD, else USD\'), ', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Sort order of display.', 'MODULE_PAYMENT_ONLINKPAYMENT_SORT_ORDER', '0', 'Sort order of display. Lowest is displayed first.', '6', '0', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, use_function, set_function, date_added) values ('Payment Zone', 'MODULE_PAYMENT_ONLINKPAYMENT_ZONE', '0', 'If a zone is selected, only enable this payment method for that zone.', '6', '2', 'tep_get_zone_class_title', 'tep_cfg_pull_down_zone_classes(', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, use_function, date_added) values ('Set Order Status', 'MODULE_PAYMENT_ONLINKPAYMENT_ORDER_STATUS_ID', '0', 'Set the status of orders made with this payment module to this value', '6', '0', 'tep_cfg_pull_down_order_statuses(', 'tep_get_order_status_name', now())");
	  tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Webservice URL', 'MODULE_PAYMENT_ONLINKPAYMENT_WEBSERVICE', 'https://vws.onlink.vn/gateway', 'Onlink webservice URL', '6', '0', now())");
	  tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Webservice WSDL URL', 'MODULE_PAYMENT_ONLINKPAYMENT_WSDLWEBSERVICE', 'https://vws.onlink.vn/gateway?wsdl', 'Onlink webservice WSDL URL', '6', '0', now())");
	  tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Transaction Type (1(now)/2)', 'MODULE_PAYMENT_ONLINKPAYMENT_TRANSACTION_TYPE', '1', 'Transaction type', '6', '0', now())");
	  tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Payment Method (INTERNET/SMS/EMAIL)', 'MODULE_PAYMENT_ONLINKPAYMENT_PAYMENT_METHOD', '', 'Payment method', '6', '0', now())");
    }

    function remove() {
      tep_db_query("delete from " . TABLE_CONFIGURATION . " where configuration_key in ('" . implode("', '", $this->keys()) . "')");
    }

    function keys() {
      return array('MODULE_PAYMENT_ONLINKPAYMENT_STATUS', 'MODULE_PAYMENT_IPAYMENT_MERCHANT_ID', 'MODULE_PAYMENT_MERCHANT_KEY', 'MODULE_PAYMENT_ONLINKPAYMENT_CURRENCY', 'MODULE_PAYMENT_ONLINKPAYMENT_SORT_ORDER', 'MODULE_PAYMENT_ONLINKPAYMENT_ZONE', 'MODULE_PAYMENT_ONLINKPAYMENT_ORDER_STATUS_ID','MODULE_PAYMENT_ONLINKPAYMENT_WEBSERVICE','MODULE_PAYMENT_ONLINKPAYMENT_WSDLWEBSERVICE', 'MODULE_PAYMENT_ONLINKPAYMENT_TRANSACTION_TYPE', 'MODULE_PAYMENT_ONLINKPAYMENT_PAYMENT_METHOD');
    }

	//vunh added
	function sendOrder($orderInfos) {
	  $orderInfos['transactionTypeID'] = MODULE_PAYMENT_ONLINKPAYMENT_TRANSACTION_TYPE;	  
  	  $soapClient = new nusoap_client(MODULE_PAYMENT_ONLINKPAYMENT_WSDLWEBSERVICE, true, false, array(), CACHE_PATH);
	  $soapClient->soap_defencoding = 'UTF-8';
	  $soapClient->decode_utf8 = false;
	  $soapClient->setCurlOption("timeout", 100);
	  $soapClient->setCurlOption('curl', CURLOPT_SSL_VERIFYHOST, 0);
	  $soapClient->setCurlOption('curl', CURLOPT_SSL_VERIFYPEER, 0);
	  $soapClient->setCurlOption('curl', CURLOPT_CAPATH, MY_CAPATH);
	  $soapOptions = array('namespace' =>DAB_NAMESPACE, 'trace' => 1);//var_dump($soapClient);
	  $result= $soapClient->call('sendOrder',array("merchantID"=>MODULE_PAYMENT_IPAYMENT_MERCHANT_ID,"merchantKey"=>MODULE_PAYMENT_MERCHANT_KEY, 'orderInfo'=>$orderInfos),$soapOptions);
	  return $result;
	}

	function tep_session_register_payment_info() {
		$payment_info = array('onlinkpayment_card_issuer' => $HTTP_POST_VARS['onlinkpayment_card_issuer'],
							   'onlinkpayment_card_holder_name' => $HTTP_POST_VARS['onlinkpayment_card_holder_name'],
							   'onlinkpayment_card_holder_id' => $HTTP_POST_VARS['onlinkpayment_card_holder_id'],
							   'onlinkpayment_card_holder_cif' => $HTTP_POST_VARS['onlinkpayment_card_holder_cif'],
					    	   'onlinkpayment_card_number' => $HTTP_POST_VARS['onlinkpayment_card_number_1'].$HTTP_POST_VARS['onlinkpayment_card_number_2'].$HTTP_POST_VARS['onlinkpayment_card_number_3'].$HTTP_POST_VARS['onlinkpayment_card_number_4'],
							   'onlinkpayment_card_number_1' => $HTTP_POST_VARS['onlinkpayment_card_number_1'],
		                       'onlinkpayment_card_number_2' => $HTTP_POST_VARS['onlinkpayment_card_number_2'],
		                       'onlinkpayment_card_number_3' => $HTTP_POST_VARS['onlinkpayment_card_number_3'],
		                       'onlinkpayment_card_number_4' => $HTTP_POST_VARS['onlinkpayment_card_number_4'],
							   'onnlinkpayment_expires_month' => $HTTP_POST_VARS['onnlinkpayment_expires_month'],
                    		   'onlinkpayment_expires_year' => $HTTP_POST_VARS['onlinkpayment_expires_year'],
							   'onlinkpayment_card_address' => $HTTP_POST_VARS['onlinkpayment_card_address'],
                    		   'onlinkpayment_card_cvv' => $HTTP_POST_VARS['onlinkpayment_card_cvv'],
							   'onlinkpayment_card_billing_name' => $HTTP_POST_VARS['onlinkpayment_card_billing_name'],
                     		   'onlinkpayment_card_billing_id' => $HTTP_POST_VARS['onlinkpayment_card_billing_id'],
							   'onlinkpayment_card_billing_address' => $HTTP_POST_VARS['onlinkpayment_card_billing_address'],
		                       'onlinkpayment_card_billing_phone' => $HTTP_POST_VARS['onlinkpayment_card_billing_phone'],
							   'onlinkpayment_card_billing_email' => $HTTP_POST_VARS['onlinkpayment_card_billing_email'],
                    		   'onlinkpayment_card_shipping_name' => $HTTP_POST_VARS['onlinkpayment_card_shipping_name'],
							   'onlinkpayment_card_shipping_id' => $HTTP_POST_VARS['onlinkpayment_card_shipping_id'],
                     		   'onlinkpayment_card_shipping_address' => tep_address_format($order->delivery['format_id'], $order->delivery, 1, ' ', ' '),
							   'onlinkpayment_card_shipping_phone' => $HTTP_POST_VARS['onlinkpayment_card_shipping_phone'],
							   'onlinkpayment_card_shipping_email' => $HTTP_POST_VARS['onlinkpayment_card_shipping_email'],
							   'onlinkpayment_payment_method' => $HTTP_POST_VARS['onlinkpayment_payment_method'],
							   'onlinkpayment_original_current_short_name' => "VND",
                               'onlinkpayment_original_amount' => $order->info['total'],
							   'redirect_url' => tep_href_link(FILENAME_CHECKOUT_PROCESS, '', 'SSL', true),
                               'silent_error_url' => tep_href_link(FILENAME_CHECKOUT_PAYMENT, 'payment_error=' . $this->code . '&onlinkpayment_card_holder_name=' . urlencode($HTTP_POST_VARS['onlinkpayment_card_holder_name']), 'SSL', true),
							   'onlinkpayment_terms_agree' => $HTTP_POST_VARS['onlinkpayment_terms_agree']);	  	  
		tep_session_register('payment_info');
	}
  }
?>
